package com.echo.fm.user.role;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.echo.fm.content.systemAction.model.SystemAction;
import com.echo.fm.platform.auth.model.RoleAuth;
import com.echo.fm.platform.database.IHibernatePersistence;
import com.echo.fm.user.role.model.Role;
import com.echo.util.ErrorException;
import com.echo.util.ResultUtils;

@Repository("rolePersistence")
public class DaoRolePersistencImpl implements IRolePersistence {
	@Autowired
	@Qualifier("hibernatePersistence")

	private IHibernatePersistence hibernatePersistence;

	public void setHibernatePersistence(IHibernatePersistence hibernatePersistence) {
		this.hibernatePersistence = hibernatePersistence;
	}

	public void createRole(Role role) throws ErrorException {
		hibernatePersistence.create(role);

	}

	public void deleteRole(Role role) throws ErrorException {
		hibernatePersistence.delete(role);

	}

	public void updateRole(Role role) throws ErrorException {
		hibernatePersistence.update(role);
	}

	public Role searchRoleById(String roleId) throws ErrorException {
		Object object = hibernatePersistence.searchById(Role.class, Restrictions.eq("id", Integer.valueOf(roleId)));

		if (object instanceof Role) {
			return (Role) object;
		}
		return null;

	}

	public List<Role> searchRoleList(List<Criterion> criterions) throws ErrorException {

		@SuppressWarnings("unchecked")
		List<Role> roleList = (List<Role>) hibernatePersistence.searchList(Role.class, criterions, Order.asc("id"));
		return roleList;
	}

	public SystemAction searchSystemAction(String id) throws ErrorException {
		Object object = hibernatePersistence.searchById(SystemAction.class, Restrictions.eq("id", id));

		if (object instanceof SystemAction) {
			return (SystemAction) object;
		}
		return null;
	}

	public void insertRoleAuth(RoleAuth roleAuth) throws ErrorException {

		hibernatePersistence.create(roleAuth);

	}

	public void deleteRoleAuth(RoleAuth roleAuth) throws ErrorException {
		hibernatePersistence.delete(roleAuth);

	}

	public List<RoleAuth> searchRoleAuthIdByRoleId(List<Criterion> criterions) throws ErrorException {

		@SuppressWarnings("unchecked")
		List<RoleAuth> roleAuthList = (List<RoleAuth>) hibernatePersistence.searchList(RoleAuth.class, criterions);
		return roleAuthList;
	}

	public void deleteRoleAuthByRoleId(Integer roleId) throws ErrorException {
		String sql = "delete from role_auth where role_id=" + roleId;
		hibernatePersistence.deletewithSQL(sql);

	}

	public void insertRoleAuth(List<RoleAuth> ralist) throws ErrorException {

		Session session = null;
		Transaction tx = null;
		try {
			session = hibernatePersistence.openSession();
			tx = session.beginTransaction();

			for (RoleAuth roleAuth : ralist) {
				session.saveOrUpdate(roleAuth);
			}
			tx.commit();

		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			hibernatePersistence.closeSession(session);
		}

	}

}
